package com.hmall.trade.listener;

import com.hmall.trade.constants.MQConstants;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class PayStatusListener {

    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange(name = "pay.direct", type = ExchangeTypes.DIRECT),
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        orderService.markOrderPaySuccess(orderId);
        System.out.println("-------使用PayStatusListener----更新订单状态");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.DELAY_ORDER_QUE_NAME, durable = "true"),
            exchange = @Exchange(value = MQConstants.DELAY_EXCHANGE_NAME,delayed = "true", type = ExchangeTypes.DIRECT),
            key = MQConstants.DELAY_ORDER_KEY))
    public void updateCancelStatus(Long orderId){
        //先根据id获取订单 判断订单的状态 如果状态是未支付再修改
        Order order = orderService.getById(orderId);
        if (order == null) {
            log.error("订单不存在");
            return;
        }
        if (order.getStatus() == 1) {
            order.setStatus(5);
            orderService.updateById(order);
        }
        System.out.println("-------使用PayStatusListener----更新订单状态");
    }
}